<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;
use Qiniu\Auth;
use Qiniu\Storage\UploadManager;

class Video extends Base
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $id=input("id");
        $usermodel=model("Video");
        $list=$usermodel->index();
        $this->assign("list",$list);
        $this->assign("id",$id);
        return $this->fetch();
    }
    //素材
    public function fodder()
    {
        $where=[];
        $id=input("id");
        $usermodel=model("Video");
        $list=db('special_task')->paginate(2);
        $this->assign("list",$list);
        $this->assign("id",$id);
        return $this->fetch();
    }
    public function fodderIndex($id)
    {
        $find = db( "special_task" )->where( ['id'=>$id] )->find();
        $this->assign( "find",$find );
        return $this->fetch();
    }
    public function fodderIndexs($id)
    {
        $find = db( "special_task" )->where( ['id'=>$id] )->find();
        $this->assign( "find",$find );
        return $this->fetch();
    }
    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        $list=db( "special_task" )->select();
        $this->assign("list",$list);
        return $this->fetch();
    }
    public function fodderCreate()
    {
        //
        return $this->fetch();
    }
    public function upload()
    {
        $file = request()->file('file');
        // 要上传图片的本地路径
        $filePath = $file->getRealPath();
        $ext = pathinfo($file->getInfo('name'), PATHINFO_EXTENSION);  //后缀
        // 上传到七牛后保存的文件名
        $key = substr(md5($file->getRealPath()), 0, 5) . date('YmdHis') . rand(0, 9999) . '.' . $ext;
        // 需要填写你的 Access Key 和 Secret Key
        // 构建鉴权对象
        $accessKey = "6jDpNaFaqlbu4yaqIbNEVik5Xcu2idiNZKuR_WIy";
        $secretKey = "Pu3AqEZdNXSZk1vNks7hLelsKqXRw1fFcElqJXlZ";
        $auth = new Auth($accessKey, $secretKey);
        // 要上传的空间
        $bucket = "1810php";
        //域名
        $domain = "qv7d7onu9.hb-bkt.clouddn.com";
        $token = $auth->uploadToken($bucket);
        // 初始化 UploadManager 对象并进行文件的上传
        $uploadMgr = new UploadManager();
        // 调用 UploadManager 的 putFile 方法进行文件的上传
        list($ret, $err) = $uploadMgr->putFile($token, $key, $filePath);
        if ($err !== null) {
            return ["err" => 1, "msg" => $err, "data" => ""];
        } else {
            //返回图片的完整URL
            $imgPath = 'http://' . $domain . '/' . $key;
            //赋值
            return json(['code' => 0, 'data' => $imgPath, 'type' => 1, 'name' => $key]);
        }
    }
    public function audio()
    {
        $file =request()->file('file');
        $info=$file->move('upload/');
        if($info){
            $path='http://jiaoyu.com/upload/'.$info->getSaveName();
            return json(["code"=>0,"msg"=>'上传成功',"data"=>$path]);
        }else{
            return json(["code"=>1,"msg"=>'上传失败',"data"=>'']);
        }
    }
    public function fodderSave(Request $request)
    {
        $inputdata=input();
//        $UsercateModel = model( "Video" );
//        $res = $UsercateModel->addData( $inputdata );
        $res=db("special_task")->insert($inputdata);
        if($res){
            $this->success("添加成功",url("Video/fodder"));
        }else{
            $this->error("添加失败");
        }
    }
    public function fodderEdit($id)
    {
        $find = db( "special_task" )->where( ['id'=>$id] )->find();
        $this->assign( "find",$find );
        return $this->fetch();
    }
    public function fodderUpdate(Request $request, $id)
    {
        $inputdata = input();
        $res = db( 'special_task' )->where( ['id'=>$id] )->update( $inputdata );
        if( $res ){
            $this->success( "修改成功", url( 'Video/fodder' ) );
        }else{
            $this->error( "修改失败" );
        }
    }
    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function remove()
    {
        $id= input('id');
        if (db('special_task')->where('id',$id)->delete()) {
            $this->success('删除成功',url("video/fodder"));
        } else {
            $this->error('删除失败');
        }
    }
    public function delete()
    {
        $id= input('id');
        if (db('video')->where('id',$id)->delete()) {
            $this->success('删除成功',url("video/index"));
        } else {
            $this->error('删除失败');
        }
    }
    public function getsu()
    {
        return $this->fetch();
    }

}
